package chapter02;

import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;

public class DeleteDuplicates82 {
    /**
     * 双指针
     * 申请一个头节点headNode 工作指针p指向头节点 p.next=null
     * head作为链表首元节点
     * 1).出现重复节点 head.next!=null&&head.val==head.next.val
     *   跳过重复节点 while(head.next!=null&&head.val==head.next.val){head=head.next}
     *   最后head指向重复节点的最后一个节点 也跳过 head=head.next
     * 2).否则 将head节点连接到p上
     *     p.next=head
     *     p=p.next
     *     head=head.next
     * p后继节点置为null p.next=null
     * 最后返回headNode.next
     */
    public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
   }
    public ListNode deleteDuplicates(ListNode head) {
        ListNode headNode=new ListNode(-1);
        ListNode p=headNode;
        headNode.next=null;
        while (head!=null){
            if(head.next!=null&&head.val==head.next.val){
                while (head.next!=null&&head.val==head.next.val){
                    head=head.next;
                }
                head=head.next;
            }else{
                p.next=head;
                p=p.next;
                head=head.next;
            }
        }
        p.next=null;
        return headNode.next;
    }
}
